

#include "OgreApplication.h"
#include "FileOps.h"



KeyboardMouseHandler::KeyboardMouseHandler(OgreApplication* ogre){
	this->ogreapp = ogre;
	this->lWindowHandle = KeyboardMouseHandler::getWindowHandle(ogre->getMainWindow());
	this->ogre_window = ogre->getMainWindow();
	this->ogre_init_settings = ogre->getInitSettings();
	this->initNull();
	this->init();
}

KeyboardMouseHandler::~KeyboardMouseHandler(){

	std::cout << "Destroying KeyboardMouseHandler\n";
	
	std::cout << "Destroying Keyboard and MouseAlloc\n";
	
	delete this->keyboard_allocation;
	delete this->mouse_button_allocation;

	if(this->lMouse != NULL){
		std::cout << "Destroying Mouse\n";
		this->lInputManager->destroyInputObject(this->lMouse);
		//delete this->lMouse;
	}

	this->unload();

	if(this->lKeyboard != NULL){
		std::cout << "Destroying Keyboard\n";
		this->lInputManager->destroyInputObject(this->lKeyboard);
		//delete this->lKeyboard;
	}

	if(this->lJoystick != NULL){
		std::cout << "Destroying Joystick\n";
		this->lInputManager->destroyInputObject(this->lJoystick);
	}

	if(this->lInputManager != NULL){
		std::cout << "Destroying  InputManager\n";
		this->lInputManager->destroyInputSystem(this->lInputManager);
		//delete this->lInputManager;
	}

}

void KeyboardMouseHandler::initNull(){
	this->lInputManager = NULL;
	this->lKeyboard = NULL;
	this->lMouse = NULL;
	this->lJoystick = NULL;

	this->deltatime = 0;
	this->mouse_delta_x = 0.0f;
	this->mouse_delta_y = 0.0f;

	this->keyboard_allocation = NULL;
	this->mouse_button_allocation = NULL;
	
	this->mouse_axis_function = "";
	this->mouse_inverted_y = false;
	
	this->joystick_axis_x_state = 0.0f;
	this->joystick_axis_y_state = 0.0f;
	this->joystick_axis_z_state = 0.0f;
	this->joystick_axis_z_rotation_state = 0.0f;
	this->joysick_cooliehat_state = -1;

	this->isloaded = false;
	this->interchar = ':';
	this->endchar = ';';

	this->isactive = true;
	this->functionfiring = true;
	//controllabel

	

}

void KeyboardMouseHandler::init(){

		

	OIS::ParamList lSpecialParameters;
	lSpecialParameters.insert(std::make_pair(std::string("WINDOW"), this->lWindowHandle));

	this->lInputManager = OIS::InputManager::createInputSystem(lSpecialParameters);

	if(this->ogre_init_settings->capture_keyboard){
	this->lKeyboard = static_cast<OIS::Keyboard*>(this->lInputManager->createInputObject(OIS::OISKeyboard,true));
	}
	
	if(this->ogre_init_settings->capture_mouse){
	this->lMouse	= static_cast<OIS::Mouse*>(this->lInputManager->createInputObject(OIS::OISMouse,true));
	}
	
	if(this->ogre_init_settings->capture_joystick){
		this->lJoystick	= static_cast<OIS::JoyStick*>(this->lInputManager->createInputObject(OIS::OISJoyStick,true));
	}

	//load std keyboard.cfg file 
	if(this->ogre_init_settings->relative_path.length() <= 0)return;
	std::string helppath = this->ogre_init_settings->relative_path+"\\config\\keyboard.cfg"; 

	if(FileOps::exists(helppath.c_str()))this->loadFile(helppath.c_str());
}

std::string KeyboardMouseHandler::getWindowHandle(Ogre::RenderWindow* window){
		size_t windowHandle = 0;
		window->getCustomAttribute("WINDOW", &windowHandle);

			std::string windowsHandleAsString="";
		{
			std::ostringstream windowHndStr;
			windowHndStr << windowHandle;
			windowsHandleAsString = windowHndStr.str();
		}
return windowsHandleAsString;
}

void KeyboardMouseHandler::handleInputs(unsigned long dtime){
	
	if(! this->functionfiring)return;
	this->deltatime = dtime;
	
	if(this->lKeyboard != NULL){
		this->lKeyboard->capture();
		this->handleKeyboard();
	
	}

	if(this->lMouse != NULL){
		this->lMouse->capture();
		this->handleMouse();
	}

	if(this->lJoystick != NULL){
		this->lJoystick->capture();
		this->handleJoystick();
	}
}

void KeyboardMouseHandler::handleKeyboard(){

	if(this->keyboard_allocation != NULL){	
		
		

		for(int i = 0; i < this->keyboard_allocation->size();i++){
			KeyboardValues* current = this->keyboard_allocation->at(i);
			bool keydown = this->lKeyboard->isKeyDown(current->id);
			
			bool firefunction = false;
			switch(current->mode){
				case Keymode::HOLD:firefunction = keydown;break;
				case Keymode::ONPRESS:firefunction = ((keydown)&&(! current->pressed));break;
				case Keymode::ONRELEASE:firefunction = ((! keydown)&&(current->pressed));break;
				case Keymode::RELEASE:firefunction = ((! keydown)&&(! current->pressed));break;
			}
			if(firefunction) this->Functions(current->functionname);
			current->pressed = keydown;
		}
	}
}

void KeyboardMouseHandler::handleMouse(){
	//handle axis
	
	this->mouse_delta_x = -1*this->lMouse->getMouseState().X.rel;
	this->mouse_delta_y = this->lMouse->getMouseState().Y.rel;

	if(! this->mouse_inverted_y) this->mouse_delta_y = this->mouse_delta_y*-1;

	if(this->mouse_axis_function.compare("") != 0)this->Functions(this->mouse_axis_function);

	//buttons

	if(this->mouse_button_allocation != NULL){	
		
		for(int i = 0; i < this->mouse_button_allocation->size();i++){
			MouseValues current = *this->mouse_button_allocation->at(i);
			bool keydown = this->lMouse->getMouseState().buttonDown(current.id);
			
			bool firefunction = false;
			switch(current.mode){
				case Keymode::HOLD:firefunction = keydown;break;
				case Keymode::ONPRESS:firefunction = ((keydown)&&(! current.pressed));break;
				case Keymode::ONRELEASE:firefunction = ((! keydown)&&(current.pressed));break;
				case Keymode::RELEASE:firefunction = ((! keydown)&&(! current.pressed));break;
			}
			if(firefunction) this->Functions(current.functionname);
			current.pressed = keydown;
		}
	}


}

void KeyboardMouseHandler::handleJoystick(){

	//implement heeeeere



}

void KeyboardMouseHandler::Functions(std::string name){

	//keyboard
	//std::cout << "fireing function" << name << "\n";
		//programm_befehle
	
	if(!this->functionfiring)return;

	if(name.compare("EXIT_BY_CLOSING") == 0){
		this->ogreapp->MainLoopManualAbort();
		return;
	}
	
		//debug_camera_ctrls

	if(name.compare("MOVE_DEBUG_CAMERA_RIGHT") == 0){
		Ogre::Camera* cam = this->ogreapp->getSceneManager()->getCamera("PlayerCamera");

		

		if(cam != NULL) cam->moveRelative(Ogre::Vector3(this->deltatime*this->ogre_init_settings->debug_camera_move_speed*this->ogre_init_settings->METER,0,0));
		return;
	}

	if(name.compare("MOVE_DEBUG_CAMERA_LEFT") == 0){
		Ogre::Camera* cam = this->ogreapp->getSceneManager()->getCamera("PlayerCamera");

		if(cam != NULL) cam->moveRelative(Ogre::Vector3(this->deltatime*this->ogre_init_settings->debug_camera_move_speed*this->ogre_init_settings->METER*-1,0,0));
		return;
	}

	if(name.compare("MOVE_DEBUG_CAMERA_FORWARD") == 0){
			Ogre::Camera* cam = this->ogreapp->getSceneManager()->getCamera("PlayerCamera");

		if(cam != NULL) cam->moveRelative(Ogre::Vector3(0,0,this->deltatime*this->ogre_init_settings->debug_camera_move_speed*this->ogre_init_settings->METER*-1));
		return;
	}

	if(name.compare("MOVE_DEBUG_CAMERA_BACKWARD") == 0){
		Ogre::Camera* cam = this->ogreapp->getSceneManager()->getCamera("PlayerCamera");

		if(cam != NULL) cam->moveRelative(Ogre::Vector3(0,0,this->deltatime*this->ogre_init_settings->debug_camera_move_speed*this->ogre_init_settings->METER));
		return;
	}

	if(name.compare("MOVE_DEBUG_CAMERA_UP") == 0){
		Ogre::Camera* cam = this->ogreapp->getSceneManager()->getCamera("PlayerCamera");

		if(cam != NULL) cam->moveRelative(Ogre::Vector3(0,this->deltatime*this->ogre_init_settings->debug_camera_move_speed*this->ogre_init_settings->METER,0));
		return;
	}

	if(name.compare("MOVE_DEBUG_CAMERA_DOWN") == 0){
		Ogre::Camera* cam = this->ogreapp->getSceneManager()->getCamera("PlayerCamera");

		if(cam != NULL) cam->moveRelative(Ogre::Vector3(0,this->deltatime*this->ogre_init_settings->debug_camera_move_speed*this->ogre_init_settings->METER*-1,0));
		return;
	}
	
	if(name.compare("MOVE_DEBUG_CAMERA_SPEEDDOWN") == 0){
		this->ogre_init_settings->debug_camera_move_speed = 0.01f;

		return;
	}
	
	if(name.compare("MOVE_DEBUG_CAMERA_SPEEDUP") == 0){
		this->ogre_init_settings->debug_camera_move_speed = 0.05f;

		return;
	}

	//mouse

	if(name.compare("MOVE_DEBUG_CAMERA_VIEW_MOUSE") == 0){
	Ogre::Camera* cam = this->ogreapp->getSceneManager()->getCamera("PlayerCamera");

	if(cam != NULL){ 
		
		cam->yaw(Ogre::Degree(this->deltatime*this->mouse_delta_x*this->ogre_init_settings->debug_camera_rotate_speed));
		cam->pitch(Ogre::Degree(this->deltatime*this->mouse_delta_y*this->ogre_init_settings->debug_camera_rotate_speed));
	}
		return;	
	}

	//sounddebug

	if(name.compare("SOUND_VEL_++") == 0){
	
		OgreApplication::soundtest_velocity = OgreApplication::soundtest_velocity - 50.0f;
		std::cout << "Put velocity to: " << OgreApplication::soundtest_velocity << "m/s \n";


	return;
	}

	if(name.compare("HINTTEST") == 0){
		this->ogreapp->getMenuHandler()->hint("Hallo Welt!");
		return;
	}

	std::cout << "No Function: "+name+" found in KeyboardMouseHandler::Functions(std::string name);";
}

//keyboard allocs


bool KeyboardMouseHandler::isLoaded(){
	return this->isloaded;
}

void KeyboardMouseHandler::save(const char* path){
	//std::string neu = "\n";
	const char* neu = "\n";
	FileOps::write(path,(BYTE*) neu,1);
	
	if(this->mouse_axis_function.compare("") != 0){
		std::string hardware = "MOUSEAXIS";
		hardware = hardware+this->interchar+this->mouse_axis_function+this->interchar;
		if(this->mouse_inverted_y){
		hardware = hardware+"1";
		}
		else{
		hardware = hardware + "0";
		}
		hardware = hardware + this->endchar+"\n";

		FileOps::write(path,(BYTE*)hardware.c_str(),hardware.length());
	}

	for(int i = 0;  i < this->mouse_button_allocation->size();i++){
		MouseValues* current = this->mouse_button_allocation->at(i);
		std::string hardware = "MOUSE";
		hardware = hardware+this->interchar+KeyboardMouseHandler::getStringFromMouseCode(current->id)+this->interchar+current->functionname;
		hardware = hardware+this->interchar+KeyboardMouseHandler::getStringFromMode(current->mode)+this->endchar+"\r\n";
		FileOps::append(path,(BYTE*)hardware.c_str(),hardware.length());
	}
	
	for(int i = 0;  i < this->keyboard_allocation->size();i++){
		KeyboardValues* current = this->keyboard_allocation->at(i);
		std::string hardware = "KEYBOARD";
		hardware = hardware+this->interchar+KeyboardMouseHandler::getStringFromKeyCode(current->id)+this->interchar+current->functionname;
		hardware = hardware+this->interchar+KeyboardMouseHandler::getStringFromMode(current->mode)+this->endchar+"\r\n";
		FileOps::append(path,(BYTE*)hardware.c_str(),hardware.length());
	}


}

void KeyboardMouseHandler::addManual(std::string hardware,std::string id,std::string functionname,std::string mode){

	std::cout << "Adding Button> hardware: " <<hardware << " id: "<< id << " functionname: " <<functionname << " mode: " << mode << "\n"; 

	if(hardware.compare("MOUSE")==0){

		if(this->mouse_button_allocation == NULL)this->mouse_button_allocation = new std::vector<MouseValues*>();
		
		MouseValues* current = new MouseValues();
		current->pressed = false;
		current->id = KeyboardMouseHandler::getMouseButtonIDFromString(id);
		current->functionname = functionname;
		current->mode = KeyboardMouseHandler::getModeFromString(mode);
		this->mouse_button_allocation->push_back(current);
	}
		
	if(hardware.compare("KEYBOARD")==0){

		if(this->keyboard_allocation == NULL)this->keyboard_allocation = new std::vector<KeyboardValues*>();
		
		KeyboardValues* current = new KeyboardValues();
		current->pressed = false;
		current->id = KeyboardMouseHandler::getKeyCodeFromString(id);
		current->functionname = functionname;
		current->mode = KeyboardMouseHandler::getModeFromString(mode);
		this->keyboard_allocation->push_back(current);
	}

}

void KeyboardMouseHandler::loadFile(const char* path){

	std::cout << "loading file: " << path << "\n";

	BYTE* data = new BYTE[FileOps::getFileSize(path)];
	std::vector<std::string>* help = new std::vector<std::string>();
	if(FileOps::read(path,(BYTE*)data,FileOps::getFileSize(path))){

	const char* ret = (const char*) data;
	int count = 0;
	std::string buffer = "";
	
	
	while(count < strlen(ret)){
		

		if(ret[count] == '\n' || ret[count] == '\r')
		{count++;continue;}
		if(ret[count] == this->endchar){
			int count2 = 0;
			help->clear();
			help->begin();
			std::string buffer2 = "";
			int size = buffer.size();
			
			while(count2 < buffer.size()){
				
				if(buffer[count2] == this->interchar || count2 +1 == buffer.size()){
						
					if(buffer.size() == count2+1)buffer2 = buffer2+buffer.c_str()[count2];
					
					std::string help3 (buffer2);	
					
						help->push_back(help3);

				buffer2 = "";
				}
				else{
					buffer2 = buffer2 + buffer.c_str()[count2];
				}
				count2++;
			}

			

			if(help->size() != 0){
			if(help->at(0).compare("MOUSEAXIS")==0 && help->size() == 3){
				this->mouse_axis_function = help->at(1);
				if(help->at(2).c_str()[0] == '0'){
					this->mouse_inverted_y = false;
				}
				else{
					this->mouse_inverted_y = true;
				}
			}
			
			if(help->at(0).compare("MOUSE")==0 && help->size() == 4){
				this->addManual("MOUSE",help->at(1),help->at(2),help->at(3));
			}

			if(help->at(0).compare("KEYBOARD")==0 && help->size() == 4){
				this->addManual("KEYBOARD",help->at(1),help->at(2),help->at(3));
			}

			}

			buffer = "";
		}
		else{
			
	buffer = buffer + ret[count];
	}
		
	count++;
	}
	
	
	}
delete [] data;
delete help;
this->isloaded = true;
}

void KeyboardMouseHandler::unload(){
	if(! this->isloaded) return;

	if(this->keyboard_allocation != NULL){
		while(this->keyboard_allocation->size() != 0){
			KeyboardValues* current =  this->keyboard_allocation->at(this->keyboard_allocation->size()-1);
				delete current;
				this->keyboard_allocation->pop_back();
		}
		delete this->keyboard_allocation;
		this->keyboard_allocation = NULL;
	}

	if(this->mouse_button_allocation != NULL){
	while(this->mouse_button_allocation->size() != 0){
		MouseValues* current =  this->mouse_button_allocation->at(this->mouse_button_allocation->size()-1);
				delete current;
				this->mouse_button_allocation->pop_back();
		}
		delete this->mouse_button_allocation;
		this->mouse_button_allocation = NULL;
	}




	this->isloaded = false;
}


std::string KeyboardMouseHandler::getStringFromMode(Keymode mode){

	std::string ret = "";
	
	switch(mode){
		case Keymode::HOLD:ret = "HOLD";break;
		case Keymode::ONPRESS:ret = "ONPRESS";break;
		case Keymode::ONRELEASE:ret = "ONRELEASE";break;
		case Keymode::RELEASE:ret = "RELEASE";break;
	}

	return ret;
}

Keymode KeyboardMouseHandler::getModeFromString(std::string mode){
	Keymode ret;

	if(0 == mode.compare("HOLD"))ret = Keymode::HOLD;
	if(0 == mode.compare("ONPRESS"))ret = Keymode::ONPRESS;
	if(0 == mode.compare("ONRELEASE"))ret = Keymode::ONRELEASE;
	if(0 == mode.compare("RELEASE"))ret = Keymode::RELEASE;
	return ret;
}

std::string KeyboardMouseHandler::getStringFromMouseCode(OIS::MouseButtonID code){

	std::string ret = "";
	
	if(code == OIS::MouseButtonID::MB_Left)ret = "MB_LEFT";
	if(code == OIS::MouseButtonID::MB_Right)ret = "MB_RIGHT";
	if(code == OIS::MouseButtonID::MB_Middle)ret = "MB_MIDDLE";
	if(code == OIS::MouseButtonID::MB_Button3)ret = "MB_3";
	if(code == OIS::MouseButtonID::MB_Button4)ret = "MB_4";
	if(code == OIS::MouseButtonID::MB_Button5)ret = "MB_5";
	if(code == OIS::MouseButtonID::MB_Button6)ret = "MB_6";
	if(code == OIS::MouseButtonID::MB_Button7)ret = "MB_7";

	return ret;
}

OIS::MouseButtonID KeyboardMouseHandler::getMouseButtonIDFromString(std::string code){
	
	
	if(0 == code.compare("MB_LEFT"))return OIS::MouseButtonID::MB_Left;
	if(0 == code.compare("MB_RIGHT"))return OIS::MouseButtonID::MB_Right;
	if(0 == code.compare("MB_MIDDLE"))return OIS::MouseButtonID::MB_Middle;
	if(0 == code.compare("MB_3"))return OIS::MouseButtonID::MB_Button3;
	if(0 == code.compare("MB_4"))return OIS::MouseButtonID::MB_Button4;
	if(0 == code.compare("MB_5"))return OIS::MouseButtonID::MB_Button5;
	if(0 == code.compare("MB_6"))return OIS::MouseButtonID::MB_Button6;
	if(0 == code.compare("MB_7"))return OIS::MouseButtonID::MB_Button7;

	return OIS::MB_Button7;
}

std::string KeyboardMouseHandler::getStringFromKeyCode(OIS::KeyCode code){
	

	if(code == OIS::KeyCode::KC_0)return "0";
	if(code == OIS::KeyCode::KC_1)return "1";
	if(code == OIS::KeyCode::KC_2)return "2";
	if(code == OIS::KeyCode::KC_3)return "3";
	if(code == OIS::KeyCode::KC_4)return "4";
	if(code == OIS::KeyCode::KC_5)return "5";
	if(code == OIS::KeyCode::KC_6)return "6";
	if(code == OIS::KeyCode::KC_7)return "7";
	if(code == OIS::KeyCode::KC_8)return "8";
	if(code == OIS::KeyCode::KC_9)return "9";
	
	
	if(code == OIS::KeyCode::KC_A)return "a";
	if(code == OIS::KeyCode::KC_B)return "b";
	if(code == OIS::KeyCode::KC_C)return "c";
	if(code == OIS::KeyCode::KC_D)return "d";
	if(code == OIS::KeyCode::KC_E)return "e";
	if(code == OIS::KeyCode::KC_F)return "f";
	if(code == OIS::KeyCode::KC_G)return "g";
	if(code == OIS::KeyCode::KC_H)return "h";
	if(code == OIS::KeyCode::KC_I)return "i";
	if(code == OIS::KeyCode::KC_J)return "j";
	if(code == OIS::KeyCode::KC_K)return "k";
	if(code == OIS::KeyCode::KC_L)return "l";
	if(code == OIS::KeyCode::KC_M)return "m";
	if(code == OIS::KeyCode::KC_N)return "n";
	if(code == OIS::KeyCode::KC_O)return "o";
	if(code == OIS::KeyCode::KC_P)return "p";
	if(code == OIS::KeyCode::KC_Q)return "q";
	if(code == OIS::KeyCode::KC_R)return "r";
	if(code == OIS::KeyCode::KC_S)return "s";
	if(code == OIS::KeyCode::KC_T)return "t";
	if(code == OIS::KeyCode::KC_U)return "u";
	if(code == OIS::KeyCode::KC_V)return "v";
	if(code == OIS::KeyCode::KC_W)return "w";
	if(code == OIS::KeyCode::KC_X)return "x";
	if(code == OIS::KeyCode::KC_Y)return "y";
	if(code == OIS::KeyCode::KC_Z)return "z";
	
	if(code == OIS::KeyCode::KC_APOSTROPHE)return "#";
	if(code == OIS::KeyCode::KC_BACKSLASH)return "\\";
	if(code == OIS::KeyCode::KC_RETURN)return "ENTER";
	if(code == OIS::KeyCode::KC_TAB)return "TAB";
	if(code == OIS::KeyCode::KC_LSHIFT)return "LSHIFT";
	if(code == OIS::KeyCode::KC_RSHIFT)return "RSHIFT";
	if(code == OIS::KeyCode::KC_GRAVE)return "^";
	if(code == OIS::KeyCode::KC_LCONTROL)return "LCONTROL";
	if(code == OIS::KeyCode::KC_RCONTROL)return "RCONTROL";
	if(code == OIS::KeyCode::KC_LMENU)return "LMENU";
	if(code == OIS::KeyCode::KC_RMENU)return "RMENU";
	if(code == OIS::KeyCode::KC_SPACE)return "SPACE";
	if(code == OIS::KeyCode::KC_BACK)return "BACKSPACE";
	if(code == OIS::KeyCode::KC_COMMA)return ",";
	if(code == OIS::KeyCode::KC_DECIMAL)return ".";
	if(code == OIS::KeyCode::KC_MINUS)return "-";
	if(code == OIS::KeyCode::KC_OEM_102)return "<";
	
	if(code == OIS::KeyCode::KC_ESCAPE)return "ESC";
	if(code == OIS::KeyCode::KC_F1)return "F1";
	if(code == OIS::KeyCode::KC_F2)return "F2";
	if(code == OIS::KeyCode::KC_F3)return "F3";
	if(code == OIS::KeyCode::KC_F4)return "F4";
	if(code == OIS::KeyCode::KC_F5)return "F5";
	if(code == OIS::KeyCode::KC_F6)return "F6";
	if(code == OIS::KeyCode::KC_F7)return "F7";
	if(code == OIS::KeyCode::KC_F8)return "F8";
	if(code == OIS::KeyCode::KC_F9)return "F9";
	if(code == OIS::KeyCode::KC_F10)return "F10";
	if(code == OIS::KeyCode::KC_F11)return "F11";
	if(code == OIS::KeyCode::KC_F12)return "F12";

	if(code == OIS::KeyCode::KC_NUMLOCK)return "NUMLOCK";
	if(code == OIS::KeyCode::KC_NUMPAD0)return "NUM_0";
	if(code == OIS::KeyCode::KC_NUMPAD1)return "NUM_1";
	if(code == OIS::KeyCode::KC_NUMPAD2)return "NUM_2";
	if(code == OIS::KeyCode::KC_NUMPAD3)return "NUM_3";
	if(code == OIS::KeyCode::KC_NUMPAD4)return "NUM_4";
	if(code == OIS::KeyCode::KC_NUMPAD5)return "NUM_5";
	if(code == OIS::KeyCode::KC_NUMPAD6)return "NUM_6";
	if(code == OIS::KeyCode::KC_NUMPAD7)return "NUM_7";
	if(code == OIS::KeyCode::KC_NUMPAD8)return "NUM_8";
	if(code == OIS::KeyCode::KC_NUMPAD9)return "NUM_9";
	if(code == OIS::KeyCode::KC_NUMPADCOMMA)return "NUM_,";
	if(code == OIS::KeyCode::KC_NUMPADENTER)return "NUM_ENTER";
	if(code == OIS::KeyCode::KC_ADD)return "NUM_+";
	if(code == OIS::KeyCode::KC_MULTIPLY)return "NUM_*";
	if(code == OIS::KeyCode::KC_SUBTRACT)return "NUM_-";
	if(code == OIS::KeyCode::KC_DIVIDE)return "NUM_/";
	
	if(code == OIS::KeyCode::KC_INSERT)return "INSERT";
	if(code == OIS::KeyCode::KC_DELETE)return "DELETE";
	if(code == OIS::KeyCode::KC_PGUP)return "PGUP";
	if(code == OIS::KeyCode::KC_PGDOWN)return "PGDOWN";
	if(code == OIS::KeyCode::KC_UP)return "UP";
	if(code == OIS::KeyCode::KC_DOWN)return "DOWN";
	if(code == OIS::KeyCode::KC_RIGHT)return "RIGHT";
	if(code == OIS::KeyCode::KC_LEFT)return "LEFT";
	if(code == OIS::KeyCode::KC_END)return "END";

	return "";
}

OIS::KeyCode KeyboardMouseHandler::getKeyCodeFromString(std::string code){

	if(code.compare("0") == 0) return OIS::KeyCode::KC_0;
	if(code.compare("1") == 0) return OIS::KeyCode::KC_1;
	if(code.compare("2") == 0) return OIS::KeyCode::KC_2;
	if(code.compare("3") == 0) return OIS::KeyCode::KC_3;
	if(code.compare("4") == 0) return OIS::KeyCode::KC_4;
	if(code.compare("5") == 0) return OIS::KeyCode::KC_5;
	if(code.compare("6") == 0) return OIS::KeyCode::KC_6;
	if(code.compare("7") == 0) return OIS::KeyCode::KC_7;
	if(code.compare("8") == 0) return OIS::KeyCode::KC_8;
	if(code.compare("9") == 0) return OIS::KeyCode::KC_9;
	
	if(code.compare("a") == 0) return OIS::KeyCode::KC_A;
	if(code.compare("b") == 0) return OIS::KeyCode::KC_B;
	if(code.compare("c") == 0) return OIS::KeyCode::KC_C;
	if(code.compare("d") == 0) return OIS::KeyCode::KC_D;
	if(code.compare("e") == 0) return OIS::KeyCode::KC_E;
	if(code.compare("f") == 0) return OIS::KeyCode::KC_F;
	if(code.compare("g") == 0) return OIS::KeyCode::KC_G;
	if(code.compare("h") == 0) return OIS::KeyCode::KC_H;
	if(code.compare("i") == 0) return OIS::KeyCode::KC_I;
	if(code.compare("j") == 0) return OIS::KeyCode::KC_J;
	if(code.compare("k") == 0) return OIS::KeyCode::KC_K;
	if(code.compare("l") == 0) return OIS::KeyCode::KC_L;
	if(code.compare("m") == 0) return OIS::KeyCode::KC_M;
	if(code.compare("n") == 0) return OIS::KeyCode::KC_N;
	if(code.compare("o") == 0) return OIS::KeyCode::KC_O;
	if(code.compare("p") == 0) return OIS::KeyCode::KC_P;
	if(code.compare("q") == 0) return OIS::KeyCode::KC_Q;
	if(code.compare("r") == 0) return OIS::KeyCode::KC_R;
	if(code.compare("s") == 0) return OIS::KeyCode::KC_S;
	if(code.compare("t") == 0) return OIS::KeyCode::KC_T;
	if(code.compare("u") == 0) return OIS::KeyCode::KC_U;
	if(code.compare("v") == 0) return OIS::KeyCode::KC_V;
	if(code.compare("w") == 0) return OIS::KeyCode::KC_W;
	if(code.compare("x") == 0) return OIS::KeyCode::KC_X;
	if(code.compare("y") == 0) return OIS::KeyCode::KC_Y;
	if(code.compare("z") == 0) return OIS::KeyCode::KC_Z;

	if(code.compare("#") == 0) return OIS::KeyCode::KC_APOSTROPHE;
	if(code.compare("\\") == 0) return OIS::KeyCode::KC_BACKSLASH;
	if(code.compare("ENTER") == 0) return OIS::KeyCode::KC_RETURN;
	if(code.compare("TAB") == 0) return OIS::KeyCode::KC_TAB;
	if(code.compare("LSHIFT") == 0) return OIS::KeyCode::KC_LSHIFT;
	if(code.compare("RSHIFT") == 0) return OIS::KeyCode::KC_RSHIFT;
	if(code.compare("^") == 0) return OIS::KeyCode::KC_GRAVE;
	if(code.compare("LCONTROL") == 0) return OIS::KeyCode::KC_LCONTROL;
	if(code.compare("RCONTROL") == 0) return OIS::KeyCode::KC_RCONTROL;
	if(code.compare("LMENU") == 0) return OIS::KeyCode::KC_LMENU;
	if(code.compare("RMENU") == 0) return OIS::KeyCode::KC_RMENU;
	if(code.compare("SPACE") == 0) return OIS::KeyCode::KC_SPACE;
	if(code.compare("BACKSPACE") == 0) return OIS::KeyCode::KC_BACK;
	if(code.compare(",") == 0) return OIS::KeyCode::KC_COMMA;
	if(code.compare(".") == 0) return OIS::KeyCode::KC_DECIMAL;
	if(code.compare("-") == 0) return OIS::KeyCode::KC_MINUS;
	if(code.compare("<") == 0) return OIS::KeyCode::KC_OEM_102;
	
	if(code.compare("F1") == 0) return OIS::KeyCode::KC_F1;
	if(code.compare("F2") == 0) return OIS::KeyCode::KC_F2;
	if(code.compare("F3") == 0) return OIS::KeyCode::KC_F3;
	if(code.compare("F4") == 0) return OIS::KeyCode::KC_F4;
	if(code.compare("F5") == 0) return OIS::KeyCode::KC_F5;
	if(code.compare("F6") == 0) return OIS::KeyCode::KC_F6;
	if(code.compare("F7") == 0) return OIS::KeyCode::KC_F7;
	if(code.compare("F8") == 0) return OIS::KeyCode::KC_F8;
	if(code.compare("F9") == 0) return OIS::KeyCode::KC_F9;
	if(code.compare("F10") == 0) return OIS::KeyCode::KC_F10;
	if(code.compare("F11") == 0) return OIS::KeyCode::KC_F11;
	if(code.compare("F12") == 0) return OIS::KeyCode::KC_F12;
	if(code.compare("ESC") == 0) return OIS::KeyCode::KC_ESCAPE;

	if(code.compare("NUMLOCK") == 0) return OIS::KeyCode::KC_NUMLOCK;
	if(code.compare("NUM_0") == 0) return OIS::KeyCode::KC_NUMPAD0;
	if(code.compare("NUM_1") == 0) return OIS::KeyCode::KC_NUMPAD1;
	if(code.compare("NUM_2") == 0) return OIS::KeyCode::KC_NUMPAD2;
	if(code.compare("NUM_3") == 0) return OIS::KeyCode::KC_NUMPAD3;
	if(code.compare("NUM_4") == 0) return OIS::KeyCode::KC_NUMPAD4;
	if(code.compare("NUM_5") == 0) return OIS::KeyCode::KC_NUMPAD5;
	if(code.compare("NUM_6") == 0) return OIS::KeyCode::KC_NUMPAD6;
	if(code.compare("NUM_7") == 0) return OIS::KeyCode::KC_NUMPAD7;
	if(code.compare("NUM_8") == 0) return OIS::KeyCode::KC_NUMPAD8;
	if(code.compare("NUM_9") == 0) return OIS::KeyCode::KC_NUMPAD9;
	if(code.compare("NUM_,") == 0) return OIS::KeyCode::KC_NUMPADCOMMA;
	if(code.compare("NUM_ENTER") == 0) return OIS::KeyCode::KC_NUMPADENTER;
	if(code.compare("NUM_+") == 0) return OIS::KeyCode::KC_ADD;
	if(code.compare("NUM_-") == 0) return OIS::KeyCode::KC_MINUS;
	if(code.compare("NUM_*") == 0) return OIS::KeyCode::KC_MULTIPLY;
	if(code.compare("NUM_/") == 0) return OIS::KeyCode::KC_DIVIDE;

	if(code.compare("INSERT") == 0) return OIS::KeyCode::KC_INSERT;
	if(code.compare("DELETE") == 0) return OIS::KeyCode::KC_DELETE;
	if(code.compare("PGUP") == 0) return OIS::KeyCode::KC_PGUP;
	if(code.compare("PGDOWN") == 0) return OIS::KeyCode::KC_PGDOWN;
	if(code.compare("END") == 0) return OIS::KeyCode::KC_END;
	if(code.compare("UP") == 0) return OIS::KeyCode::KC_UP;
	if(code.compare("DOWN") == 0) return OIS::KeyCode::KC_DOWN;
	if(code.compare("RIGHT") == 0) return OIS::KeyCode::KC_RIGHT;
	if(code.compare("LEFT") == 0) return OIS::KeyCode::KC_LEFT;
	
	
	return OIS::KeyCode::KC_UNASSIGNED;
}

bool KeyboardMouseHandler::isActive(){
return this->isactive;
}

void KeyboardMouseHandler::setActive(bool active){
	if(!(active ^ this->isactive))return;

	if(active){
		this->activateControls();
	}
	else{
		this->deactivateControls();
	}

	
}

void KeyboardMouseHandler::activateControls(){
	if(this->lKeyboard == NULL){
	this->lKeyboard = static_cast<OIS::Keyboard*>(this->lInputManager->createInputObject(OIS::OISKeyboard,true));
	}
	
	if(this->lMouse == NULL){
	this->lMouse	= static_cast<OIS::Mouse*>(this->lInputManager->createInputObject(OIS::OISMouse,true));
	}
	
	if(this->lJoystick == NULL){
		this->lJoystick	=  static_cast<OIS::JoyStick*>(this->lInputManager->createInputObject(OIS::OISJoyStick,true));
	}
	this->isactive = true;
}

void KeyboardMouseHandler::deactivateControls(){

	if(this->lKeyboard == NULL){
		this->lInputManager->destroyInputObject(this->lKeyboard);
	}
	
	if(this->lMouse == NULL){
	this->lInputManager->destroyInputObject(this->lMouse);
	}
	
	if(this->lJoystick == NULL){
		this->lInputManager->destroyInputObject(this->lJoystick);
	}
	

	this->isactive = false;
}

bool KeyboardMouseHandler::isFunctionFiring(){
	return this->functionfiring;
}

void KeyboardMouseHandler::setFunctionFiring(bool firing){
this->functionfiring = firing;
}

std::string* KeyboardMouseHandler::getCurrentPressedButton(){

	std::string* ret = new std::string[2];
	ret[0] = "NONE";

	if(this->lMouse != NULL){
		this->lMouse->capture();
		OIS::MouseState mousestate = this->lMouse->getMouseState();
		
		
		for(int i = 0; i < 7;i++){ 
			if(mousestate.buttonDown((OIS::MouseButtonID)i)){
				ret[0] = "MOUSE";ret[1]=KeyboardMouseHandler::getStringFromMouseCode((OIS::MouseButtonID)i);return ret;
			}
		}
		
	}
	
	if(this->lKeyboard != NULL){
		this->lKeyboard->capture();
		
		for(int i = 0; i < 250;i++){ 
			
			if(this->lKeyboard->isKeyDown((OIS::KeyCode)i)){
				ret[0] = "KEYBOARD";ret[1]=KeyboardMouseHandler::getStringFromKeyCode((OIS::KeyCode)i);return ret;
			}
		}
		
	}

	

	
	return ret;
}

OIS::Mouse* KeyboardMouseHandler::getMouse(){
return this->lMouse;
}

OIS::Keyboard* KeyboardMouseHandler::getKeyboard(){
return this->lKeyboard;
}